﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.collection {
    import reprise.ui.*;
    import flash.events.*;
    import flash.display.*;
    import reprise.controls.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.popups.*;
    import net.wooga.pets.models.world.vos.actors.machines.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;

    public class SingleCollectionMediator extends AbstractPopupMediator {

        public var collectableModel:CollectableModel;
        public var computerModel:ComputerModel;
        public var userModel:UserModel;
        public var world:WorldModel;
        private var collectableVO:CollectableVO;
        private var needsCollection:Array;
        private var hasCollection:Array;
        private var hasAllItems:Boolean = true
        private var inOwnHospital:Boolean;
        private var isCommingSoon:Boolean = true
        private var hasRequiredLevel:Boolean;
        private var askButtons:Dictionary;
        private var petLevel:int;

        public function SingleCollectionMediator(){
            this.askButtons = new Dictionary();
            super();
        }
        override public function onRegister():void{
            if (this.view.collection){
                this.isCommingSoon = false;
                this.petLevel = this.computerModel.getRequiredLevelByPetName(this.view.collection.type);
                this.hasRequiredLevel = (this.petLevel <= this.userModel.level);
            };
            this.setBackground();
            super.onRegister();
            this.inOwnHospital = !(this.userModel.isVisitFriendsMode());
            this.setContent();
            this.preloadAssets();
        }
        override protected function addCloseButton():void{
        }
        override protected function onUpdateAskForGiftButton(_arg1:InfoEvent):void{
            var _local2:String = _arg1.info.request_type;
            handleRequestLimits(_local2);
        }
        override protected function hideAskButton(_arg1:String):void{
            var _local2:LabelButton = (this.askButtons[_arg1] as LabelButton);
            if (_local2){
                _local2.removeCSSClass("blueButton");
                _local2.addCSSClass("greyButton");
            };
        }
        private function addAskButton(_arg1:UIComponent, _arg2:String):void{
            var _local3:Boolean = sessionModel.getRequestLimit(_arg2);
            var _local4:String = (_local3) ? "greyButton" : "blueButton";
            var _local5:LabelButton = createButton(_arg1, ("collectableAskButton " + _local4), "global_button_ask", "myriadProFont");
            _local5.enabled = !(_local3);
            this.askButtons[_arg2] = _local5;
            _local5.setData({gemId:_arg2});
            eventMap.mapListener(_local5, MouseEvent.CLICK, this.onAskClick);
        }
        private function onAskClick(_arg1:MouseEvent):void{
            var _local2:LabelButton = (_arg1.target as LabelButton);
            var _local3:String = _local2.data().gemId;
            var _local4:InfoEvent = new InfoEvent(GameConstants.ASK_FOR_COLLECTABLE, _local3);
            dispatch(_local4);
        }
        private function setBackground():void{
            var _local1:String = (((this.hasRequiredLevel) && (!(this.isCommingSoon)))) ? "bg bg" : "bg bgSoon";
            RepriseUtils.addComponent(this.view, (_local1 + Strings.capitalize(this.view.collection.rewardPet)));
        }
        private function setContent():void{
            var _local1:String;
            if (((this.hasRequiredLevel) && (!(this.isCommingSoon)))){
                this.initHasCollection();
                this.needsCollection = this.view.collection.requires;
                this.setHeader();
                this.createItems();
            } else {
                if (this.isCommingSoon){
                    _local1 = language.getTextByKey("global_coming_soon");
                } else {
                    _local1 = language.getTextByKey("global_levelnumber", {levelnumber:this.petLevel});
                };
                this.createLockedCollection(_local1);
            };
        }
        private function createLockedCollection(_arg1:String):void{
            RepriseUtils.addComponent(this.view, "collectionLocked");
            RepriseUtils.addLabel(this.view, "showcardGothicFontOutline collectionLockedLabel", _arg1);
        }
        private function initHasCollection():void{
            this.collectableVO = this.collectableModel.getCollectableByType(this.view.collection.type);
            this.hasCollection = this.collectableVO.collected.concat();
            var _local1:int = this.collectableModel.getCollectableByType(this.collectableVO.rewardPet);
            this.hasCollection.push(_local1);
        }
        private function setHeader():void{
            var _local1:String = language.getTextByKey(((("collections_" + this.view.collection.type) + "_") + this.view.collection.rewardPet));
            RepriseUtils.addLabel(this.view, "singleCollectableHeader", _local1);
        }
        private function createItems():void{
            var _local1:UIComponent = RepriseUtils.addComponent(this.view, "container");
            this.createCollectableItem(_local1, 0);
            RepriseUtils.addComponent(_local1, "collectionPlus");
            this.createCollectableItem(_local1, 1);
            RepriseUtils.addComponent(_local1, "collectionPlus");
            this.createCollectableItem(_local1, 2);
            RepriseUtils.addComponent(_local1, "collectionPlus");
            this.createCollectableItem(_local1, 3);
            RepriseUtils.addComponent(_local1, "collectionEquals");
            this.createRewardItem(_local1, true);
            this.createOrLabel(_local1);
            this.createRewardItem(_local1, false);
        }
        private function createRewardItem(_arg1:UIComponent, _arg2:Boolean):void{
            var _local3:Boolean;
            var _local4:String;
            var _local5:String;
            var _local7:String;
            var _local6:UIComponent = RepriseUtils.addComponent(_arg1, "rewardItem");
            if (_arg2){
                _local3 = ((((this.inOwnHospital) && (this.hasAllItems))) && (this.canTradePet()));
                _local4 = "pet";
                _local5 = ("RC_Collectable_Reward_" + this.collectableVO.type);
                this.createPetExtra(_local6);
                _local7 = "collectablePetRewardContainer";
            } else {
                _local3 = ((this.inOwnHospital) && (this.hasAllItems));
                _local4 = "other";
                _local5 = (("Reward" + Strings.capitalize(this.collectableVO.rewardOther)) + "Thumb");
                _local7 = "collectableOtherRewardContainer";
                RepriseUtils.addLabel(_local6, "collectableOtherRewardLabel", this.collectableVO.rewardOtherAmount.toString());
            };
            this.addCollectableIcon(_local6, _local5, _local7);
            var _local8:String = (_local3) ? "tradeButton greenButton" : "tradeButton greyButton";
            var _local9:LabelButton = createButton(_local6, _local8, "global_button_trade", "myriadProFont");
            _local9.enabled = _local3;
            _local9.setData(_local4);
            ((_local3) && (_local9.addEventListener(MouseEvent.CLICK, this.onTradeClick)));
        }
        private function createPetExtra(_arg1:UIComponent):void{
            var target = _arg1;
            var extraContainer:UIComponent = RepriseUtils.addComponent(target, "rewardPetExtraContainer");
            var key = (("Reward" + Strings.capitalize(this.collectableVO.rewardOther)) + "Thumb");
            var iconClip:MovieClip = AssetsLibrary.getMovieClip(key);
            var icon:UIComponent = RepriseUtils.addClipComponent(extraContainer, "rewardPetExtraIcon", iconClip);
            RepriseUtils.validateComponent(icon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, 0, 0);
            });
            RepriseUtils.addLabel(extraContainer, "rewardPetExtraLabel", ("+" + this.collectableVO.rewardPetAmount));
        }
        private function canTradePet():Boolean{
            var _local2:int;
            var _local3:String;
            var _local4:IRoom;
            var _local5:KindergartenMachineVO;
            var _local1:Boolean;
            if (this.collectableVO.rewardPet == "elder"){
                _local2 = this.world.getNumberOfElders();
                _local1 = (((this.userModel.level >= 5)) && ((_local2 <= 4)));
            } else {
                if (this.collectableVO.rewardPet == "baby"){
                    _local3 = this.world.getRoomIdsOfType(GameConstants.TYPE_KINDERGARTEN)[0];
                    _local4 = (this.world.getRoomById(_local3) as IRoom);
                    _local5 = (_local4.machine as KindergartenMachineVO);
                    _local1 = ((_local5.isFree) && ((this.userModel.level >= 6)));
                };
            };
            return (_local1);
        }
        private function createCollectableItem(_arg1:UIComponent, _arg2:int):void{
            var _local6:UIComponent;
            var _local7:String;
            var _local8:String;
            var _local3:int = this.hasCollection[_arg2];
            var _local4:int = this.needsCollection[_arg2];
            var _local5 = (_local3 >= _local4);
            if (_arg2 < 3){
                _local7 = ((("RC_Collectable_" + this.view.collection.type) + "_") + (_arg2 + 1));
                _local8 = ((this.view.collection.type + "collection") + (_arg2 + 1));
            } else {
                _local7 = ("RC_Collectable_" + this.view.collection.rewardPet);
                _local8 = (this.view.collection.rewardPet + "collection");
            };
            if (_local5){
                _local6 = RepriseUtils.addComponent(_arg1, "collectedItemGreen");
                this.addCollectableIcon(_local6, _local7, "collectableItemContainer");
                this.createCollectableLabel(_local6, "collectableLabel collectableLabelGreen", _local4, _local4);
                this.addSolvedIcon(_local6);
            } else {
                this.hasAllItems = false;
                _local6 = RepriseUtils.addComponent(_arg1, "collectedItem");
                this.addCollectableIcon(_local6, _local7, "collectableItemContainer");
                this.createCollectableLabel(_local6, "collectableLabel", _local3, _local4);
                this.addAskButton(_local6, _local8);
            };
        }
        private function addCollectableIcon(_arg1:UIComponent, _arg2:String, _arg3:String):void{
            var target = _arg1;
            var key = _arg2;
            var cssClasses = _arg3;
            var iconClip:MovieClip = AssetsLibrary.getMovieClip(key);
            var icon:UIComponent = RepriseUtils.addClipComponent(target, cssClasses, iconClip);
            RepriseUtils.validateComponent(icon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, 0, 0);
            });
        }
        private function addSolvedIcon(_arg1:UIComponent):void{
            var target = _arg1;
            var solvedClip:MovieClip = AssetsLibrary.getMovieClip("SolvedIcon");
            var solvedIcon:UIComponent = RepriseUtils.addClipComponent(target, "collectionSolvedIcon", solvedClip);
            RepriseUtils.validateComponent(solvedIcon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, -1, -1);
            });
        }
        private function createOrLabel(_arg1:UIComponent):void{
            var _local2:String = language.getTextByKey("collections_reward_or");
            RepriseUtils.addLabel(_arg1, "orLabel", _local2);
        }
        private function createCollectableLabel(_arg1:UIComponent, _arg2:String, _arg3:int, _arg4:int):void{
            var _local5:String = language.getTextByKey("global_of_new", {has:_arg3, needed:_arg4});
            RepriseUtils.addLabel(_arg1, _arg2, _local5);
        }
        private function onTradeClick(_arg1:MouseEvent):void{
            var _local2:LabelButton = (_arg1.target as LabelButton);
            dispatch(new InfoEvent(EventConstants.TRADE_COLLECTION, this.view.collection.type, _local2.data()));
            dispatch(new InfoEvent(EventConstants.CLOSE_POPUP_BY_TYPE, this.view.popupType));
        }
        private function preloadAssets():void{
            var _local1:String;
            if (((this.collectableVO) && ((this.collectableVO.rewardPet == "baby")))){
                _local1 = ((this.collectableVO.type + "_") + WorldConstants.BABY);
                dispatch(new InfoEvent(EventConstants.PRELOAD_BABY, _local1.toLowerCase()));
            };
        }
        private function get view():SingleCollectionView{
            return ((viewComponent as SingleCollectionView));
        }

    }
}//package net.wooga.pets.views.popups.collection 
